package com.zb.system.controller;

import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjAccount;
import com.zb.exception.SMSException;
import com.zb.redis.RedisLoginTokenUtil;
import com.zb.result.Result;
import com.zb.service.ZjAccountService;
import com.zb.system.service.ConAccountService;
import com.zb.util.PCSmsUtil;
import com.zb.utils.TokenUtil;
import com.zb.vo.AccountInfoVo;
import com.zb.vo.AccountPasswordVo;
import com.zb.vo.RegisterAccountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 账号表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-07-18
 */
@Api(tags = "账号类接口")
@RestController
@RequestMapping("/system/account")
public class ZjAccountController {
    @Resource
    private ConAccountService conAccountService;
    @Resource
    private ZjAccountService accountService;

    @ApiOperation("注册账号")
    @PostMapping("/register")
    public Result register(@RequestBody @Valid RegisterAccountVo accountVo) throws SMSException {
        conAccountService.register(accountVo);
        return Result.success("注册成功");
    }

    @ApiOperation("未登录修改密码")
    @PutMapping("/notLoginUpdatePassword")
    public Result notLoginUpdatePassword(@RequestBody @Valid AccountPasswordVo passwordVo) throws SMSException {
        conAccountService.notLoginUpdatePassword(passwordVo);
        return Result.success("密码修改成功！");
    }

    @ApiOperation("修改密码")
    @PutMapping("/updatePassword")
    public Result updatePassword(HttpServletRequest request, @RequestBody @Valid AccountPasswordVo passwordVo) throws SMSException {
        Long accountId = TokenUtil.getAccountIdByToken(request);
        conAccountService.updatePassword(accountId, passwordVo);
        return Result.success("密码修改成功！");
    }

    @ApiOperation("变更手机号")
    @PutMapping("/updatePhone")
    public Result updatePhone(HttpServletRequest request,
                              @RequestParam @ApiParam(value = "手机号", required = true) String newPhone,
                              @RequestParam @ApiParam(value = "验证码", required = true) String code) throws SMSException {
        String accessToken = TokenUtil.getToken(request);
        AccounLoginInfo info = TokenUtil.getAccountLoginInfo(accessToken);
        conAccountService.updatePhone(info, newPhone, code);
        RedisLoginTokenUtil.setAccountLoginInfoByAccessToken(accessToken, info);
        return Result.success("变更手机号成功");
    }

    @ApiOperation("获取登录账号信息")
    @GetMapping("/getInfo")
    public Result getInfo(HttpServletRequest request) {
        Long accountId = TokenUtil.getAccountIdByToken(request);
        ZjAccount account = accountService.getById(accountId);
        return Result.success().data("id", account.getId())
                .data("phone", account.getPhone())
                .data("name", account.getName())
                .data("nickname", account.getNickname())
                .data("avatar", account.getAvatar());
    }

    @ApiOperation("根据手机号获取账号信息")
    @GetMapping("/getInfo/{phone}")
    public Result getInfo(@PathVariable String phone) {
        ZjAccount account = accountService.getPhone(phone);
        return Result.success().data("id", null != account? account.getId() : "")
                .data("phone", null != account? account.getPhone() : "")
                .data("name", null != account? account.getName() : "")
                .data("nickname", null != account? account.getNickname() : "")
                .data("avatar", null != account? account.getAvatar() : "");
    }

    @ApiOperation("更改个人信息")
    @PutMapping("/updateInfo")
    public Result updateInfo(HttpServletRequest request, @RequestBody @Valid AccountInfoVo infoVo) {
        String accessToken = TokenUtil.getToken(request);
        AccounLoginInfo info = TokenUtil.getAccountLoginInfo(accessToken);
        accountService.updateInfo(info, infoVo);

        RedisLoginTokenUtil.setAccountLoginInfoByAccessToken(accessToken, info);
        return Result.success("个人信息更新成功！");
    }
}
